//
//  UIBarButtonItem+Extension.swift
//  新浪微博
//
//  Created by MaJi on 16/4/8.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
//    UIBarButtonItem(image: <#T##UIImage?#>, style: <#T##UIBarButtonItemStyle#>, target: AnyObject?, action: <#T##Selector#>)
//    构造方法
    convenience init(title: String? = nil ,imageName: String? = nil,target: AnyObject?,action:Selector) {
        self.init()
        
//       1. 创建按钮
        let btn = UIButton()

//        2.添加按钮的图片
        if let img = imageName {
            btn.setImage(UIImage(named: img), forState: .Normal)
            btn.setImage(UIImage(named: img + "_highlighted"), forState: .Highlighted)
        }
            btn.setTitle(title, forState: .Normal)
            
            btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
//            btn.sizeToFit()
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
//        设置图片的内边距 距离文字空间大一些 (注意数字不要太大)
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5)
        //        设置按钮frame
        btn.sizeToFit()
//       3. 给按钮添加点击事件
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
//        设置字体
//        4.设置自定义视图
        customView = btn
    }
}
